

/**
 * La classe SaveGame si occupa di salvare lo stato di una partita, richiamando tutti i
 * metodi necessari per poter memorizzare all'interno di un file xml gli elementi
 * del gioco che necessitano del salvataggio del loro stato.
 * @author Lucia Mongardi
 */
import java.io.File;
import java.io.FileOutputStream;
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.jdom.output.Format;

public class SaveGame {
private  Element root = new Element("game");
private  Element playersel= new Element("players");
 Element phrases=new Element("phrases");
    Element numberel=new Element("number");
    Element lettersel=new Element("letters");
private List <Element> players= new ArrayList<Element>();
private List <String> letters=new ArrayList<String>();
private Document doc = new Document(root);
private XMLOutputter serializer;
    private FileOutputStream out;

    private int game;
     private String letterString="";
public SaveGame(){
    
}
public void setPlayers(List<Player> players){
    this.players.clear();
    
    Element number = new Element("number");

    for(int i=0; i<players.size();i++){

        
    Element player=new Element("player");
    Element name=new Element("name");
    Element manche_point=new Element("manche");
    Element game_point=new Element("gamepoint");
    Element jolly=new Element("jolly");
    if(players.get(i).isSelected())
        number.setText(""+i);
        name.setText(players.get(i).getPlayerName());
        manche_point.setText(""+players.get(i).getManchePoint());
        game_point.setText(""+players.get(i).getGamePoint());
        jolly.setText(""+players.get(i).getJollyBoolean());
       
        player.addContent(name);
        player.addContent(manche_point);
        player.addContent(game_point);
        player.addContent(jolly);
        this.players.add(player);
       
    }
    for(int i=0;i<this.players.size();i++){
        playersel.addContent(this.players.get(i));
    }
    playersel.addContent(number);
    root.addContent(playersel);
}
public void setGame(int game){
    this.game=game;
}
public void setPhrases(String letter){
   
    
   
        if(!letters.contains(letter)){
         letters.add(letter);
         letterString+=letter+",";
        }

 
    
}
public void clearPhrases(){
    letters.clear();
    letterString="";
}
public void setWheel(String elementString,boolean wheelButton,boolean passButton,boolean jollyButton){
    Element wheel=new Element("wheel");
    Element element=new Element("element");
    Element wheelButtonElement=new Element("wheelButton");
    Element passButtonElement=new Element("passButton");
    Element jollyButtonElement=new Element("jollyButton");
    element.setText(elementString);
    wheelButtonElement.setText(""+wheelButton);
    passButtonElement.setText(""+passButton);
    jollyButtonElement.setText(""+jollyButton);
    wheel.addContent(element);
    wheel.addContent(wheelButtonElement);
    wheel.addContent(passButtonElement);
    wheel.addContent(jollyButtonElement);
    root.addContent(wheel);
}
public void writeXML(){
    numberel.setText(""+game);
       lettersel.setText(letterString);
    phrases.addContent(numberel);
     phrases.addContent(lettersel);
     root.addContent(phrases);

      try {
          doc.setDocType(new DocType("game","savegame.dtd"));
     serializer= new XMLOutputter(Format.getPrettyFormat());
      
     
      serializer.output(doc, System.out);

 out = new FileOutputStream(new File( "../data/partitaSalvata.xml"));
 
  serializer.output(doc, out);
  

   JOptionPane.showMessageDialog(null, "Partita Salvata");
    }
    catch (IOException e) {
      System.err.println(e);
    }
     letters.clear();
     lettersel.removeContent();
    phrases.removeContent();
     
      playersel.removeContent();
      root.removeContent();
}

}




